package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.vo.AgeDomain;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DormitoryNotice;
import com.ruoyi.system.service.IDormitoryNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-01-07
 */
@RestController
@RequestMapping("/notices/notices")
class DormitoryNoticeController extends BaseController
{
    @Autowired
    private IDormitoryNoticeService dormitoryNoticeService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:notices:list')")
    @GetMapping("/list")
    public TableDataInfo list(DormitoryNotice dormitoryNotice)
    {
        startPage();
        List<DormitoryNotice> list = dormitoryNoticeService.selectDormitoryNoticeList(dormitoryNotice);
        return getDataTable(list);
    }
    @GetMapping("/listSs")
    public AjaxResult listSs(DormitoryNotice dormitoryNotice)
    {
        startPage();
        List<DormitoryNotice> list = dormitoryNoticeService.selectDormitoryNoticeList(dormitoryNotice);
        Map<String, List<DormitoryNotice>> collect = list.stream().collect(Collectors.groupingBy(item -> {
            return item.getNoticeSushe();
        }));
        return success(collect);
    }
    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:notices:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DormitoryNotice dormitoryNotice)
    {
        List<DormitoryNotice> list = dormitoryNoticeService.selectDormitoryNoticeList(dormitoryNotice);
        ExcelUtil<DormitoryNotice> util = new ExcelUtil<DormitoryNotice>(DormitoryNotice.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }
    @GetMapping("/age")
    public TableDataInfo seleteAge(DormitoryNotice notice)
    {
        startPage();
        List<AgeDomain> list = dormitoryNoticeService.selectNoticeAge(notice);

        return getDataTable(list);
    }
    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notices:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") String noticeId)
    {
        return success(dormitoryNoticeService.selectDormitoryNoticeByIssuer(noticeId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notices:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DormitoryNotice dormitoryNotice)
    {
        return toAjax(dormitoryNoticeService.insertDormitoryNotice(dormitoryNotice));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notices:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DormitoryNotice dormitoryNotice)
    {
        return toAjax(dormitoryNoticeService.updateDormitoryNotice(dormitoryNotice));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:notices:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{issuers}")
    public AjaxResult remove(@PathVariable String[] issuers)
    {
        return toAjax(dormitoryNoticeService.deleteDormitoryNoticeByIssuers(issuers));
    }

    @Anonymous
    @PostMapping(value = "/uploadFile")
    public String updateImgFile2(@RequestParam(value = "userId") String userId, @RequestPart MultipartFile file) throws IOException {
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tQYeeASCg5RDfwDg8HK";
        String accessKeySecret = "zYriighPTjBBBBsSfh2HztBWgaF7TH";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
        InputStream inputStream =file.getInputStream();
        Date date = new Date();



        //文件名称
        String preUuid = UUID.randomUUID().toString();
        System.out.println(preUuid);

        //第一种方法生成UUID，去掉“-”符号
        String uuid = UUID.randomUUID().toString().replace("-", "");


        String fileUrl = file.getOriginalFilename();
        PutObjectResult putObjectResult = ossClient.putObject("wumin-sushe", fileUrl, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        //返回路径
        String filePath="https://wumin-sushe"+"."+endpoint+"/"+fileUrl;

        System.out.println("上传成功...");

        return filePath;}

}
